<template>
  <div>
    <Modal
      :value="modalState"
      :title="title"
      :loading="loading"
      @on-ok="handleSubmit"
      @on-cancel="cancel"
      width="600">
      <Form
        ref="updateSaleModal"
        :model="row"
        :rules="ruleInline"
        inline
        style="width: 100%"
        :label-width="85">
        <FormItem prop="name" label="预约姓名" style="width: 250px">
          <Input type="text" v-model="row.name" placeholder="请输入预约姓名" />
        </FormItem>
        <FormItem prop="tel" label="预约手机号" style="width: 250px">
          <Input type="text" v-model="row.tel" placeholder="请输入预约手机号" />
        </FormItem>
        <FormItem prop="wechat" label="微信号" style="width: 250px">
          <Input type="text" v-model="row.wechat" placeholder="请输入微信号" />
        </FormItem>
        <FormItem prop="ymoney" label="应付金额" style="width: 250px">
          <Input type="text" v-model="row.ymoney" placeholder="请输入应付金额" />
        </FormItem>
        <FormItem prop="money" label="实付金额" style="width: 250px">
          <Input type="text" v-model="row.money" placeholder="请输入实付金额" />
        </FormItem>
        <!-- <FormItem prop="ctime" label="记录时间" style="width: 250px">
           <DatePicker v-model="row.ctime" type="date" placeholder="请输入记录时间"></DatePicker>
          <TimePicker type="time" placeholder="请选择时间"></TimePicker>
        </FormItem> -->
        <!-- <FormItem prop="muid" label="业务员ID" style="width: 250px">
          <Input type="text" v-model="row.checkuid" placeholder="请输入复核员ID" />
        </FormItem>
        <FormItem prop="puid" label="业务员上级ID" style="width: 250px">
          <Input type="text" v-model="row.checkuid" placeholder="请输入复核员ID" />
        </FormItem>
        <FormItem prop="checkuid" label="复核员ID" style="width: 250px">
          <Input type="text" v-model="row.checkuid" placeholder="请输入复核员ID" />
        </FormItem> -->
        <!-- <FormItem prop="pid" label="上级id" style="width: 250px">
          <Select v-model="row.pid" clearable placeholder="请选择上级ID">
            <Option v-for="item in pidList" :value="item.id.toString()" :key="item.id">
              {{ item.uname }}
            </Option>
          </Select>
        </FormItem> -->
        <FormItem v-show="row.id" prop="comedate" label="到司日期" style="width: 250px">
          <DatePicker type="date" v-model="row.comedate" placeholder="请输入到访日期"></DatePicker>
          <!-- <TimePicker type="time" placeholder="请选择时间"></TimePicker> -->
        </FormItem>
        <FormItem prop="type" label="类型" style="width: 250px">
          <Select v-model="row.type" clearable placeholder="请选择类型">
            <Option value="CPA">
              CPA
            </Option>
            <Option value="CPV">
              CPV
            </Option>
            <Option value="CPS">
              CPS
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="orderdate" label="预约日期" style="width: 250px">
          <DatePicker type="date" v-model="row.orderdate" placeholder="请输入预约日期"></DatePicker>
          <!-- <TimePicker type="time" v-model="row.orderdate"></TimePicker> -->
        </FormItem>
        <FormItem v-show="row.id" prop="state" label="状态" style="width: 250px">
          <Select v-model="row.state" clearable placeholder="请选择状态">
            <Option value="0">
              记录
            </Option>
            <Option value="1">
              到司复核
            </Option>
            <Option value="2">
              成单复核
            </Option>
          </Select>
        </FormItem>
        <FormItem prop="des" label="备注" style="width: 100%">
          <Input v-model="row.des" type="textarea" placeholder="请输入备注" />
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
export default {
  name: 'UpdateSaleModal',
  data () {
    return {
      title: '',
      loading: true,
      form: {
        name: '',
        tel: '',
        wechat: '',
        ctime: '',
        checkuid: '',
        comedate: '',
        orderdate: '',
        ymoney: '',
        money: '',
        type: '',
        state: '',
        des: ''
      },
      ruleInline: {
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  updated () {
    if (this.row.id && this.row.id !== '') {
      this.title = '修改预约信息'
    } else {
      this.title = '添加预约记录'
    }
  },
  computed: {
    ...mapState({
      saleList: state => state.sales.saleList,
      row: state => state.sales.row,
      modalState: state => state.modal.modalState
    })
  },
  methods: {
    ...mapActions(['updateModalState']),
    changeLoading () {
      this.loading = false
      this.$nextTick(() => {
        this.loading = true
      })
    },
    handleSubmit () {
      this.$refs.updateSaleModal.validate((valid) => {
        if (!valid) {
          return this.changeLoading()
        }
        this.$emit('update-valid', {
          id: this.row.id,
          name: this.row.name,
          tel: this.row.tel ? parseInt(this.row.tel) : '',
          wechat: this.row.wechat ? this.row.wechat : '',
          ymoney: this.row.ymoney !== '' ? parseInt(this.row.ymoney) : '',
          money: this.row.money !== '' ? parseInt(this.row.money) : '',
          // ctime: this.row.ctime ? this.$moment(this.row.ctime).format('YYYY-MM-DD') : '',
          // checkuid: this.row.checkuid ? parseInt(this.row.checkuid) : '',
          // puid: this.row.puid ? parseInt(this.row.puid) : '',
          // muid: this.row.muid ? parseInt(this.row.muid) : '',
          comedate: this.row.comedate ? this.$moment(this.row.comedate).format('YYYY-MM-DD') : '',
          orderdate: this.row.orderdate ? this.$moment(this.row.orderdate).format('YYYY-MM-DD HH:mm:ss') : '',
          type: this.row.type ? this.row.type : '',
          state: this.row.state ? parseInt(this.row.state) : '',
          des: this.row.des ? this.row.des : ''
        })
        this.$refs['updateSaleModal'].resetFields()
      })
    },
    cancel () {
      this.updateModalState(false)
      this.$refs['updateSaleModal'].resetFields()
    }
  }
}
</script>
